package com.cskaoyan.service.wx;

import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.MarketTopic;
import com.cskaoyan.bean.MarketTopicExample;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.cskaoyan.model.admin.vo.AdTopicReadVo;
import com.cskaoyan.model.admin.vo.BaseData;
import com.cskaoyan.model.admin.vo.BaseDataListVo;
import com.cskaoyan.model.vx.vo.WxTopicVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hanshuai
 * @version 1.0
 * @description @TODO
 * @date 2021/12/2 11:01
 */
@Service
public class WxTopicServiceImpl implements WxTopicService{
    @Autowired
    MarketTopicMapper marketTopicMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Override
    public BaseData topicRelated(Integer id) {
        PageHelper.startPage(1, 4);
        MarketTopicExample example = new MarketTopicExample();
        example.createCriteria().andIdNotEqualTo(id);
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(marketTopics);
        BaseData baseData = new BaseData();
        baseData.setTotal((int) pageInfo.getTotal());
        baseData.setPages(pageInfo.getPages());
        baseData.setLimit(4);
        baseData.setPage(1);
        baseData.setList(marketTopics);
        return baseData;
    }

    @Override
    public WxTopicVo topicDetail(Integer id) {
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);
        List<MarketGoods> goods = new ArrayList<>();
        for (Integer goodsId : marketTopic.getGoods()) {
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(goodsId);
            goods.add(marketGoods);
        }
        WxTopicVo wxTopicVo = new WxTopicVo();
        wxTopicVo.setTopic(marketTopic);
        wxTopicVo.setGoods(goods);
        return wxTopicVo;
    }

    @Override
    public BaseDataListVo topicList(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        MarketTopicExample topicExample = new MarketTopicExample();
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(topicExample);
        PageInfo pageInfo = new PageInfo(marketTopics);
        BaseDataListVo baseDataListVo = new BaseDataListVo();
        baseDataListVo.setList(marketTopics);
        baseDataListVo.setPages(pageInfo.getPages());
        baseDataListVo.setTotal((int) pageInfo.getTotal());
        baseDataListVo.setLimit(limit);
        baseDataListVo.setPage(page);
        return baseDataListVo;

    }
}
